#include <windows.h>
#include <tchar.h>
#include "tracelog.h"
#include "dllmain.h"
#include "resource.h"
#include "log.h"

BOOL popupSelectMenu(HWND hWnd) {
	HMENU hMenuLoad, hMenu;
	int x,y;
	hMenuLoad = LoadMenu(getInstance(), MAKEINTRESOURCE(MENU_SELECT));
	hMenu = GetSubMenu(hMenuLoad, 0); 
	RECT r;
	GetWindowRect(hWnd,&r);
	x=r.left;
	y=r.bottom;
	TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(hWnd),  NULL);
	DestroyMenu(hMenuLoad);
	return TRUE; 
} 


BOOL CALLBACK filterDialogProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
	int i;
	switch(msg){
		case WM_COMMAND:
			switch ( LOWORD(wParam) ){
				case CM_SELECT_ALL:
					for(i=0;i<32;i++){
						SendDlgItemMessage(hwnd,101+i,BM_SETCHECK,1,0);
					}
					break;
				case CM_DESELECT_ALL:
					for(i=0;i<32;i++){
						SendDlgItemMessage(hwnd,101+i,BM_SETCHECK,0,0);
					}
					break;
				case IDC_CB_SELECT:
					popupSelectMenu(GetDlgItem(hwnd, IDC_CB_SELECT));
					break;
				case IDCANCEL:
					SendMessage(hwnd,WM_CLOSE,0,0);
					break;
				case IDOK:
					unsigned long mask=0;
					for(i=0;i<32;i++){
						if (SendDlgItemMessage(hwnd,101+i,BM_GETCHECK,0,0)==BST_CHECKED){
							mask=mask|(1<<i);
						}
					}
					setMaskSet(mask);
					SendMessage(hwnd,WM_CLOSE,0,0);
					break;
			}
			return 1;   
		case WM_CLOSE:
			EndDialog(hwnd,0);
			return 1;
		case WM_INITDIALOG:
			for(i=0;i<32;i++){
				if (getMaskSet()&(1<<i)) SendDlgItemMessage(hwnd,101+i,BM_SETCHECK,1,0);
				SendDlgItemMessage(hwnd,101+i,WM_SETTEXT,0,(LPARAM)(getMaskInfo(1<<i)+2) );
			}	
			return 1;
	}
	return 0;
}

BOOL createFilterDialog(HWND parent){
	DialogBox(getInstance(),MAKEINTRESOURCE(DLG_FILTER),parent,(DLGPROC)filterDialogProc);
	return 1;
}

